package com.example.notepad.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.notepad.bean.Notepad;
import com.example.notepad.utils.DBUtils;

import java.util.ArrayList;
import java.util.List;

public class SQLiteHelper extends SQLiteOpenHelper {
    private final SQLiteDatabase db;

    public SQLiteHelper(@Nullable Context context) {
        super(context, DBUtils.DATABASE_NAME, null, DBUtils.DATABASE_VERSION);
        db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + DBUtils.TABLE_NAME + "(" +
                DBUtils.NOTEPAD_ID + " integer primary key autoincrement, " +
                DBUtils.NOTEPAD_CONTENT + " text," +
                DBUtils.NOTEPAD_TIME + " text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public boolean insert(String noteContent, String noteTime) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBUtils.NOTEPAD_CONTENT, noteContent);
        contentValues.put(DBUtils.NOTEPAD_TIME, noteTime);

        return db.insert(DBUtils.TABLE_NAME, null, contentValues) > 0;
    }

    public boolean delete(String id) {
        String sql = DBUtils.NOTEPAD_ID + "=?";
        return db.delete(DBUtils.TABLE_NAME, sql, new String[]{String.valueOf(id)}) > 0;
    }

    public boolean update(String id, String noteContent, String noteTim) {
        String sql = DBUtils.NOTEPAD_ID + "=?";
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBUtils.NOTEPAD_CONTENT, noteContent);
        contentValues.put(DBUtils.NOTEPAD_TIME, noteTim);

        return db.update(DBUtils.TABLE_NAME, contentValues, sql,
                new String[]{id}) > 0;
    }

    public boolean update(Notepad notepad) {
        String sql = DBUtils.NOTEPAD_ID + "=?";
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBUtils.NOTEPAD_CONTENT, notepad.getContent());
        contentValues.put(DBUtils.NOTEPAD_TIME, notepad.getTime());

        return db.update(DBUtils.TABLE_NAME, contentValues, sql,
                new String[]{String.valueOf(notepad.getId())}) > 0;
    }

    public List<Notepad> query() {
        ArrayList<Notepad> list = new ArrayList<>();

        Cursor cursor = db.query(DBUtils.TABLE_NAME, null, null, null,
                null, null, DBUtils.NOTEPAD_ID + " desc");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Notepad notepad = new Notepad();
                notepad.setId(cursor.getInt(cursor.getColumnIndex(DBUtils.NOTEPAD_ID)));
                notepad.setContent(cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_CONTENT)));
                notepad.setTime(cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_TIME)));

                list.add(notepad);
            }
            cursor.close();
        }
        return list;
    }
}
